﻿using BaseDll;
using CommonTools;
using Communicate;
using DBMgrLib;
using HY_UPHMgr;
using MotionIoLib;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserData;

namespace StationDemo
{
    public class UserConfig
    {
        public static bool b_退料 = false;
        public static string str_SN = "";
        public static bool b_isScan = false;

        public static void CloseHardWork()
        {
            ResetIO();
            AlarmMgr.GetIntance().StopAlarmBeet();
            TcpMgr.GetInstance().CloseAllEth();
            MotionMgr.GetInstace().Close();
            IOMgr.GetInstace().Close();
        }
        public static void ResetIO()
        {
            IO_OUT.蜂鸣.Set(false);
            IO_OUT.指示灯红.Set(false);
            IO_OUT.指示灯绿.Set(false);
            IO_OUT.双启动灯A.Set(false);
            IO_OUT.双启动灯B.Set(false);

        }
        public static void AddStation()
        {

            Station_Load station_Load = new Station_Load(StationMgr.GetInstance().GetStation(Station.上料站.ToString()), new Param_Load(), new Point_Load(), new Config_Load());
            StationMgr.GetInstance().AddStation(new StationForm(station_Load, station_Load.station_Param, station_Load.station_Point, station_Load.station_Config, null), Station.上料站.ToString(), station_Load);

            Station_Table station_Table = new Station_Table(StationMgr.GetInstance().GetStation(Station.转盘站.ToString()), new Param_Table(), new Point_Table(), new Config_Table());
            UIPage page_Table = new Form_Manual转盘(Station.转盘站.ToString());
            page_Table.Dock = DockStyle.Fill;
            page_Table.Show();
            StationMgr.GetInstance().AddStation(new StationForm(station_Table, station_Table.station_Param, station_Table.station_Point, station_Table.station_Config, page_Table), Station.转盘站.ToString(), station_Table);

            Station_标定站 station_标定站 = new Station_标定站(StationMgr.GetInstance().GetStation(Station.标定站.ToString()), new Param_标定站(), new Point_标定站(), new Config_标定站());
            StationMgr.GetInstance().AddStation(new StationForm(station_标定站, station_标定站.station_Param, station_标定站.station_Point, station_标定站.station_Config, null), Station.标定站.ToString(), station_标定站);

            Station_治具 station_治具A站 = new Station_治具(StationMgr.GetInstance().GetStation(Station.治具A站.ToString()), new Param_治具(), new Point_治具(), new Config_治具());
            StationMgr.GetInstance().AddStation(new StationForm(station_治具A站, station_治具A站.station_Param, station_治具A站.station_Point, station_治具A站.station_Config, null), Station.治具A站.ToString(), station_治具A站);

            Station_治具 station_治具B站 = new Station_治具(StationMgr.GetInstance().GetStation(Station.治具B站.ToString()), new Param_治具(), new Point_治具(), new Config_治具());
            StationMgr.GetInstance().AddStation(new StationForm(station_治具B站, station_治具B站.station_Param, station_治具B站.station_Point, station_治具B站.station_Config, null), Station.治具B站.ToString(), station_治具B站);


        }
        public static bool Funtion_运行前处理()
        {
            //List<string> list_Err = HY_AllConsumables.ConsumeFactory.Ins._Consume.GetWarningList();
            //if (list_Err != null && list_Err.Count > 0)
            //{
            //    string msg = "";
            //    foreach (var item in list_Err)
            //    {
            //        msg += $"{item},";
            //    }
            //    if (AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.耗材使用完毕, new string[] { "继续运行", "退出运行" }, new string[] { $"耗材使用完毕:{msg}" }) == WaranResult.Custom2)
            //        return false;
            //}
            StationStateHelper.OnTest_ShowStep("系统", "正在自动运行", LogType.Info);
            return true;
        }
        public static void Funtion_暂停时处理()
        {
            StationStateHelper.OnTest_ShowStep("系统", "正在暂停中", LogType.Warn);
        }
        public static void Funtion_恢复时处理()
        {
            StationStateHelper.OnTest_ShowStep("系统", "正在自动运行", LogType.Info);
        }
        public static void Funtion_停止时处理()
        {
            UserConfig.ResetIO();
            UPHTimeMgr.Instance.StopAll();

            GlobalVariable.g_StationState = StationState.StationStateEmg;
            StationStateHelper.OnTest_ShowStep("系统", "点击停止", LogType.Err);
        }
        public static bool Function_全机回零()
        {
            try
            {

                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                Station_Load station_load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
                Station_治具 station_A = (Station_治具)StationMgr.GetInstance().GetStation(Station.治具A站.ToString());
                Station_治具 station_B = (Station_治具)StationMgr.GetInstance().GetStation(Station.治具B站.ToString());

                Form1.OnTest_ShowHome("开始回零", 10);
                bool bResult = true;


                try
                {
                    //bResult &= station_load.Function_气缸复位();

                    Form1.OnTest_ShowHome("SocketA 开始回原", 20);
                    bResult &= station_A.Function_工站回零();
                    Form1.OnTest_ShowHome("SocketB 开始回原", 30);
                    bResult &= station_B.Function_工站回零();
                    Form1.OnTest_ShowHome("转盘 开始回原", 40);
                    bResult &= station_Table.Function_工站回零();
                }
                catch (Exception)
                {
                    bResult &= false;
                }


                if (!bResult)
                {
                    return false;
                }

                Form1.OnTest_ShowHome("回零完成", 100);
                return true;
            }
            catch (Exception ex)
            {
                UIMessageBox.Show(ex.Message);
                return false;
            }
        }
        public static void InitOtherParamter()
        {


            if (true)
            {
                //启用数据库
                DeviceDataCollect.GetInstance().Function_SaveInDataBase(true);
                DeviceDataCollect.GetInstance().DeleteHistoryFile(30);//30天
                SQLiteMgr.GetInstance().Init();
            }
            else
            {
                //不启用数据库
                DeviceDataCollect.GetInstance().Function_SaveInDataBase(false);
                DeviceDataCollect.GetInstance().DeleteHistoryFile(30);//30天

            }
            UPHTimeMgr.Instance.InitData();
            UPHTimeMgr.Instance.SetDirPath(@"D:\HY System", "AONI02");

            NGCountMgr.GetInstance().LoadNGType(Station.标定站.ToString(), Enum.GetNames(typeof(ClibraNGType)));
            NGCountMgr.GetInstance().LoadNGType(Station.治具A站.ToString(), Enum.GetNames(typeof(SocketNGType)));
            NGCountMgr.GetInstance().LoadNGType(Station.治具B站.ToString(), Enum.GetNames(typeof(SocketNGType)));
        }
    }
}
